﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HanGu.Services.Interface;
using HanGu.Entity;

namespace HanGu.Services
{
    public class EveryDayWisdomService : BaseService, IEveryDayWisdom
    {

        public List<EveryDayWisdom> GetAll()
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                return obj.EveryDayWisdom.OrderByDescending(c => c.UpdateTime).ToList();
            }
        }

        public List<EveryDayWisdom> GetAll(UniversallySearchParam searchparma)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                var query = obj.EveryDayWisdom.Where(c => 1 == 1);
                if (!string.IsNullOrEmpty(searchparma.Ttile))
                {
                    query = query.Where(c => c.Remark == searchparma.Ttile);
                }
                if (!string.IsNullOrEmpty(searchparma.Creator))
                {
                    query = query.Where(c => c.Author == searchparma.Creator);
                }
                if (searchparma.StarDateTime.HasValue)
                {
                    query = query.Where(c => c.UpdateTime >= searchparma.StarDateTime);
                }
                if (searchparma.EndDateTime.HasValue)
                {
                    query = query.Where(c => c.UpdateTime <= searchparma.EndDateTime);
                }
                query = base.Paging<EveryDayWisdom, DateTime?>(c => c.UpdateTime, query, searchparma.Pagination);

                return query.ToList();
            }
        }

        public EveryDayWisdom GetByID(int id)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                return obj.EveryDayWisdom.Where(c => c.ID == id).FirstOrDefault();
            }
        }
        public EveryDayWisdom GetByRadom()
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                var model = obj.EveryDayWisdom.Select(c => new { NewID = Guid.NewGuid() ,c }).OrderBy(c => c.NewID).FirstOrDefault();
                return model.c;
            }
        }

        public bool Add(EveryDayWisdom model)
        {
            try
            {
                this.Add<EveryDayWisdom>(model);
                return true;
            }
            catch { return false; }
        }

        public bool Update(EveryDayWisdom model)
        {
            try
            {
                this.Update<EveryDayWisdom>(model);
                return true;
            }
            catch { return false; }
        }

        public bool Delete(int id)
        {
            try
            {
                this.Delete<EveryDayWisdom>(c => c.ID == id);
                return true;
            }
            catch { return false; }
        }


    }
}
